package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.modular.system.entity.Kms;
import cn.stylefeng.guns.sys.modular.system.entity.KmsApp;
import cn.stylefeng.guns.sys.modular.system.mapper.KmsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YuJiBiao
 * @version: 1.0
 * @Date 2020/7/8 16:54
 **/
@Service
public class KmsService extends ServiceImpl<KmsMapper, Kms> {

    @Autowired
    private  JdbcTemplate jdbcTemplate;

    @Autowired
    private KmsAppService kmsAppService;
    /**
     * 根据条件查询用户列表
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:45
     */
    public Page<Map<String, Object>> selectKmsList(Integer index,String backId) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.selectKmsList(page,index,backId);
    }

    public void uploadFiles(List<Long> idList, MultipartFile file,String name){
    }

    public List<Kms> downList(String beginTime,String endTime) {
        return  this.baseMapper.selectKmsListData(beginTime,endTime);
    }


    /**
     * 根据条件查询用户列表
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:45
     */
    public Page<Map<String, Object>> selectkmsDistributionList(Integer index,String backId) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.selectkmsDistributionList(page,index,backId);
    }


    public Page<Map<String, Object>> connectionKeyList(Integer index, String backId, LoginUser user) {
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.connectionKeyList(page,index,backId,user.getId());

    }

    public List<Map> selectKmsListById(String token) {
        QueryWrapper<KmsApp> srcwrapper = new QueryWrapper<>();
        srcwrapper.eq("token", token);
        KmsApp one = kmsAppService.getOne(srcwrapper);
        return  this.baseMapper.selectKmsListById(one.getId());
    }

    /**
     *
     */
    public List<Double> getChartsData() {
        List<Double> map = Lists.newArrayList();
        String sql = "SELECT count(*) as count   from sys_qnl_key where `status` = '已使用'";
        String sql2 = "SELECT pool_size as count   from sys_qnl_key where `status` = '正常' GROUP BY pool_size\n";
        Map<String, Object> ysy = jdbcTemplate.queryForMap(sql);
        Map<String, Object> wsy = jdbcTemplate.queryForMap(sql2);
        Map<String,Object> ysyData = Maps.newHashMap();
        Map<String,Object> wsyData = Maps.newHashMap();
        //
        Long count = (Long) ysy.get("count");
//        long l = 4093 / count;
        double c=count/4096;
        BigDecimal a = new BigDecimal(count);
        BigDecimal b = new BigDecimal(4096);
        BigDecimal divide = a.divide(b);
        BigDecimal d =  new BigDecimal("0.01");
        if(divide.compareTo(d) ==-1) {
            map.add(0.01);
        }
        else {
            BigDecimal multiply = divide.multiply(new BigDecimal(100));
            BigDecimal bd = multiply.setScale(2, RoundingMode.HALF_UP);
            map.add(bd.doubleValue());
        }
        map.add(0.05);
        map.add(0.05);
        map.add(0.05);
        return map;
    }

    public List<Kms> bakKms() {
       return this.baseMapper.bakKms();
    }
}
